package com.ruoyi.wechart.controller;

import com.ruoyi.common.constant.DeviceConstant;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.device.domain.SysDeviceBorrow;
import com.ruoyi.device.domain.SysDeviceNew;
import com.ruoyi.device.service.ISysDeviceBorrowService;
import com.ruoyi.device.service.ISysDeviceNewService;
import com.ruoyi.device.service.ISysDeviceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/wechart/device")
@RestController
public class WxDeviceController extends BaseController {
    @Autowired
    private ISysDeviceTypeService typeService;
    @Autowired
    private ISysDeviceNewService deviceService;
    @Autowired
    private ISysDeviceBorrowService borrowService;

    @GetMapping("/typeList")
    public AjaxResult typeList(){
        return success(typeService.selectDeviceTypeList(null));
    }

    @GetMapping("/deviceList")
    public AjaxResult deviceList(SysDeviceNew deviceNew){
        List<SysDeviceNew> sysDeviceNews = deviceService.selectSysDeviceNewList(deviceNew);
//        sysDeviceNews = sysDeviceNews.stream().map(device -> {
//            device.setDeviceImg("D:\\ruoyi\\ruoyi202401\\uploadPath\\upload".concat(device.getDeviceImg()));
//            return device;
//        }).collect(Collectors.toList());
        return success(sysDeviceNews);
    }

    @GetMapping("/{deviceId}")
    public AjaxResult equipQueryId(@PathVariable("deviceId") Long deviceId){
        SysDeviceNew sysDeviceNew = deviceService.selectSysDeviceNewByDeviceId(deviceId);
        return success(sysDeviceNew);
    }

//    @PutMapping("/{deviceId}")
//    public AjaxResult borrowApplication(@PathVariable("deviceId") Long deviceId){
//        SysDeviceNew sysDeviceNew = deviceService.selectSysDeviceNewByDeviceId(deviceId);
//        System.out.println(SecurityUtils.getUserId());
//        return success(sysDeviceNew);
//    }

    @PutMapping
    public AjaxResult borrowApplication(@RequestBody SysDeviceBorrow sysDeviceBorrow){
        borrowService.insertWeixSysDeviceBorrow(sysDeviceBorrow);
        return success(sysDeviceBorrow);
    }

    @PostMapping("/returnDevice")
    public AjaxResult returnDevice(@RequestBody SysDeviceBorrow sysDeviceBorrow){
        //修改归还状态为申请归还
        sysDeviceBorrow.setReturnStatus(DeviceConstant.DEVICE_BORROW_STATUS_APPLY_REPAIRS);
        borrowService.updateSysDeviceBorrowWeixin(sysDeviceBorrow);
        return success("申请成功");
    }

    /*@PostMapping("/returnDevice")
    public AjaxResult returnDevice(@RequestBody SysDeviceBorrow sysDeviceBorrow){
        SysDeviceNew device = new SysDeviceNew();
        device.setDeviceId(sysDeviceBorrow.getDeviceId());
        device.setDeviceStatus(0);
        sysDeviceBorrow.setReturnStatus(3);//就当申请归还就为归还成功
        deviceService.updateSysDeviceNew(device);
        borrowService.updateSysDeviceBorrowWeixin(sysDeviceBorrow);
        return success("申请成功");
    }*/

    @GetMapping("/borrowList")
    public AjaxResult borrowList(SysDeviceBorrow borrow){
        List<SysDeviceBorrow> sysDeviceBorrowList = borrowService.selectSysDeviceBorrowList(borrow);
        return success(sysDeviceBorrowList);
    }
}
